package com.example.itcompanyinterview.tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.example.itcompanyinterview.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.text.util.Linkify;

import android.graphics.Color;

import android.widget.TextView;

public class AboutDialog extends Dialog {
	private static Context mContext = null;

	public AboutDialog(Context context) {
		super(context, R.style.AboutDialog);
		mContext = context;
	}
	
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.about);

		TextView tv = (TextView) findViewById(R.id.legal_text);
		tv.setText(readRawTextFile(R.raw.legal));
		tv = (TextView) findViewById(R.id.info_text);
		tv.setText(Html.fromHtml(readRawTextFile(R.raw.info)));
		tv.setTextColor(Color.WHITE);
		tv.setLinkTextColor(Color.BLUE);
		Linkify.addLinks(tv, Linkify.ALL);
		
	}

	public static String readRawTextFile(int id) {
		InputStream inputStream = mContext.getResources().openRawResource(id);
		InputStreamReader in = new InputStreamReader(inputStream);
		BufferedReader buf = new BufferedReader(in);
		String line;
		StringBuilder text = new StringBuilder();
		boolean has_content = false;
		try {
			while ((line = buf.readLine()) != null) {
				text.append(line + "\n");
				has_content = true;
			}
		} catch (IOException e) {
			return null;
		}
		
		if(has_content) {
			earseLastNewLineChar(text);
		}
		
		return text.toString();
	}
	
	private static void earseLastNewLineChar(StringBuilder text) {
		if(text.charAt(text.length() - 2) == '\n') {
			text.setCharAt(text.length() - 2, '\0');
		}
	}
}